 //如果登录 改变布局
function changeLogin(){
    let login=JSON.parse(localStorage.getItem('login')||"[]")
    let iflogin=document.querySelector('.iflogin')
    if(login!=''){
     
       iflogin.innerHTML=`用户<a href='#'>${login.username}</a>`
       iflogin.nextSibling.innerHTML=`<a href='./cart.html'>我的订单</a>`
    }else{
        iflogin.innerHTML=`<a href="./login.html">登录</a>`
        iflogin.nextSibling.innerHTML=`<a href="./register.html">请注册</a>`
    }   
}
changeLogin()


//退出登录
let exit=document.querySelector('.exit')
exit.onclick=function(){
    if(confirm('退出登录?')){
        localStorage.removeItem('login')
        location.reload();
    }
}

//掉接口
getlist()
function getlist(){
    
    let keyword=decodeURIComponent(location.search.split('=')[1])
    
    axios({
        method:'get',
        url:'http://api.yuguoxy.com/api/shop/search',
        params:{
            keyword
        },
    }).then(response=>{
        let data=response.data
       
        let{resultCode,resultInfo} = data
        if(resultCode == 1 ){
            let goodlist = resultInfo.list
           showList(goodlist)
        }else{
            ul.innerHTML=`<h1>没有此商品</h1>`
        }
    })
}
let ul=document.querySelector('.remodlist').querySelector('ul')
function showList(goodlist){
   
    let str=goodlist.map(item=>{
        return `
        <li data-id=${item.id}>
        <div class="remodlist_img">
            <img src="${item.picture}" alt="">
        </div>
        <div class="recomlist_text">
          <div class="oneline">  
              <span>价保618</span>
            <span class="spanp">${item.shop}</span></div>
        
            <span class="recomprice">$${item.price}</span>
        </div>
     
    </li>
        `
    }).join('')

   
    ul.innerHTML=str
    
    clickGood()
}

//点击商品跳转详情
function clickGood(){
    let productOul=document.querySelector('.remodlist').querySelector('ul')
    
    let lis=productOul.children;
    console.log(lis);
    [...lis].forEach(item=>{
        item.addEventListener('click',function(){
            let goodId=this.getAttribute('data-id')
            location.href=`../pages/detail.html?id=${goodId}`
        })
    })
}

//搜索
let svg=document.querySelector('.svg')
svg.onclick=function(){
    let search=document.querySelector('.search')
    let val=search.value;
    if(val==''){
        alert('请输入搜索内容')
    }else{
        location.href=`../pages/find.html?keyword=${val}`
       
    }
   
    
}
